package com.hyzx.qbasic.admin.domain.repository.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.CacheKeyConst;
import com.hyzx.qbasic.admin.model.po.AdminUserInfoPO;
import com.hyzx.qbasic.admin.model.po.AdminUserPO;
import com.hyzx.qbasic.admin.model.po.AdminUserTokenPO;
import com.hyzx.qbasic.admin.domain.dao.AdminUserMapper;
import com.hyzx.qbasic.admin.domain.dao.OrgMapper;
import com.hyzx.qbasic.admin.domain.repository.IAdminUserRepository;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * Created by 黄江华 on 2017/9/21
 */

@Repository
@Scope(proxyMode = ScopedProxyMode.TARGET_CLASS) //使用缓存需加此注解
public class AdminUserRepositoryImpl implements IAdminUserRepository {
    @Autowired
    private AdminUserMapper adminUserMapper;

    @Autowired
    private OrgMapper orgMapper;

    @Override
    public AdminUserInfoPO getUserInfoById(int userId) {
        return adminUserMapper.getUserInfoById(userId);
    }

    @Override
    public AdminUserInfoPO getUserInfoByName(String userName) {
        return adminUserMapper.getUserInfoByName(userName);
    }

    @Override
    public AdminUserTokenPO getUserToken(String userName) {
        return adminUserMapper.getUserToken(userName);
    }

    @Override
    public AdminUserPO getFullUserInfo(String userName) {
        return adminUserMapper.getFullUserInfo(userName);
    }

    @Override
    public PageInfo<AdminUserInfoPO> getPagedUserList(int pageNum, int pageSize, String userName, Integer roleId) {
        PageInfo pageInfo = PageHelper.startPage(pageNum, pageSize)
                .doSelectPageInfo(() -> adminUserMapper.getPagedUserList(userName, roleId));

        return pageInfo;
    }

    @Override
    @Transactional
    public boolean addUser(AdminUserPO user, List<Integer> roleList, Integer orgId) {
        int addUserResult = adminUserMapper.addUser(user);
        if (CollectionUtils.isNotEmpty(roleList))
            adminUserMapper.addUserRoles(user.getUserId(), roleList);

        if (orgId != null) {
            orgMapper.addUserOrg(user.getUserId(), orgId);
        }

        return addUserResult > 0;
    }

    @Override
    @Transactional
    public boolean deleteUser(List<Integer> userIdList) {
        int delUserResult = adminUserMapper.deleteUser(userIdList);
        adminUserMapper.deleteUserRoles(userIdList);
        orgMapper.deleteUserOrgs(userIdList);

        return delUserResult > 0;
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = CacheKeyConst.PERMISSION_LIST_CACHE_NAME, key = "#user.userId")
    public boolean editUser(AdminUserPO user, List<Integer> roleList, Integer orgId) {
        int editUserResult = adminUserMapper.editUser(user);

        adminUserMapper.deleteUserRoles(Arrays.asList(user.getUserId()));
        if (CollectionUtils.isNotEmpty(roleList))
            adminUserMapper.addUserRoles(user.getUserId(), roleList);

        orgMapper.deleteUserOrgs(Arrays.asList(user.getUserId()));
        if (orgId != null) {
            orgMapper.addUserOrg(user.getUserId(), orgId);
        }

        return editUserResult > 0;
    }

    @Override
    public boolean resetPass(AdminUserPO user) {
        return adminUserMapper.resetPass(user) > 0;
    }

    @Override
    public List<AdminUserInfoPO> getUserListByRole(List<Integer> roleIdList) {
        return adminUserMapper.getUserListByRole(roleIdList);
    }

    @Override
    public List<AdminUserInfoPO> getMultiUserInfo(List<Integer> userIdList) {
        return adminUserMapper.getMultiUserInfo(userIdList);
    }
}
